//
//  SearchContentTableView.m
//  dhfsAPP
//
//  Created by Devil on 2017/8/7.
//  Copyright © 2017年 Devil. All rights reserved.
//

#import "SearchContentTableView.h"

@implementation SearchContentTableView

#pragma mark - 构造函数
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        self.delegate = self;
        self.dataSource = self;
    }
    
    return self;
}

#pragma mark - UITableViewDelegate&UITableViewDataSource实现
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.searchContentArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return HEIGHT(217);
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return HEIGHT(82);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HEIGHT(82))];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH(20), 0, 0, 0)];
    
    title.text = @"搜索结果";
    title.textColor = RGB(51, 51, 51);
    title.font = [UIFont fontWithName:@"PingFangSC-Regular" size:HEIGHT(32)];
    
    [title sizeToFit];
    title.center = CGPointMake(title.center.x, headerView.center.y);
    
    [headerView addSubview:title];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT(81), SCREEN_WIDTH, HEIGHT(1))];
    
    lineView.backgroundColor = RGB(221, 221, 221);
    
    [headerView addSubview:lineView];
    
    return headerView;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *str = @"cell";
    UITableViewCell *cell = nil;
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    else
    {
        while ([cell.contentView.subviews lastObject] != nil)
        {
            [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
    
    cell.selectionStyle = NO;
    
    cell.backgroundColor = RGB(221,221, 221);
    
    SearchContentCellView *cellView = [[SearchContentCellView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HEIGHT(200)) ImgUrl:self.searchContentArray[indexPath.row][@"pic_src"] Title:self.searchContentArray[indexPath.row][@"name"] Time:self.searchContentArray[indexPath.row][@"duration"] PlayNum:self.searchContentArray[indexPath.row][@"play"]];
    
    [cell addSubview:cellView];

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.searchContentTableViewDelegate respondsToSelector:@selector(clickItem:)]) {
        [self.searchContentTableViewDelegate clickItem:(int)indexPath.row];
    }
}
@end
